/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import controller.NewDocumentController;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import model.Base;
import net.miginfocom.swing.MigLayout;

/**
 *
 * @author Pierre-Antoine
 */
public class NewDocumentWindow extends JDialog{

    //<editor-fold defaultstate="collapsed" desc="nom des composants">
        public static final String TXT_CATEGORY = "txt_category";
        public static final String TXT_YEAR = "txt_year";
        public static final String TXT_TITLE = "txt_title";
        public static final String TXT_AUTHORS = "txt_authors";
        public static final String TXT_EDITOR = "txt_editor";
        public static final String BTN_CREATE = "btn_create";
        public static final String BTN_CANCEL = "btn_cancel";
        public static final String CBB_TYPE = "cbb_type";
	//</editor-fold>

	private JLabel lblYearError = new JLabel("");
	private JLabel lblTitleError = new JLabel("");
	private JLabel lblAuthorsError = new JLabel("");
	private JLabel lblEditorError = new JLabel("");	
	private JLabel lblCategory = new JLabel("Catégorie");
	private JLabel lblYear = new JLabel("Année");
	private JLabel lblTitle = new JLabel("titre");
	private JLabel lblAuthors = new JLabel("Auteur");
	private JLabel lblEditor = new JLabel("Editeur");
	private JLabel lblInfo = new JLabel("Création d'un nouveau document");
	private JLabel lblInfoCreation = new JLabel(" ");
	private JTextField txtYear = new JTextField(9);
	private JTextField txtTitle = new JTextField(9);
	private JTextField txtAuthors = new JTextField(9);
	private JTextField txtEditor = new JTextField(9);
	
	private JPanel pnlBook = new JPanel(new MigLayout("insets 0 0 0 0"));
	private JPanel pnlCD = new JPanel(new MigLayout("insets 0 0 0 0"));
	private JPanel pnlDVD = new JPanel(new MigLayout("insets 0 0 0 0"));
	private JPanel pnlMagazine = new JPanel(new MigLayout("insets 0 0 0 0"));
	private JPanel pnlNewsPaper = new JPanel(new MigLayout("insets 0 0 0 0"));

	private JLabel lblNbPageBook = new JLabel("Nb. de page");
	private JLabel lblTypeBook = new JLabel("Type");
	private JLabel lblLenghtCD = new JLabel("Durée");
	private JLabel lblTypeCD = new JLabel("Type");
	private JLabel lblLenghtDVD = new JLabel("Durée");
	private JLabel lblTypeDVD = new JLabel("Type");
	private JLabel lblNbPageMag = new JLabel("Nb. de page");
	private JLabel lblDayDate = new JLabel("Date du jour");
	
	private JTextField txtNbPageBook = new JTextField(9);
	private JTextField txtTypeBook = new JTextField(9);
	private JTextField txtLenghtCD = new JTextField(9);
	private JTextField txtTypeCD = new JTextField(9);
	private JTextField txtLenghtDVD = new JTextField(9);
	private JTextField txtTypeDVD = new JTextField(9);
	private JTextField txtNbPageMag = new JTextField(9);
	private JTextField txtDayDate = new JTextField(9);
	
	private JComboBox cbbCategory;
	private JButton btnCancel = new JButton("Annuler");
	private JButton btnAdd = new JButton("Ajouter");

	private NewDocumentController controller;

	public NewDocumentWindow(Base model) {
		this.controller = new NewDocumentController(model, this);
		this.setLayout(new MigLayout());

		this.cbbCategory = new JComboBox(model.getCategories().toArray());
		
		this.txtYear.setName(NewDocumentWindow.TXT_YEAR);
		this.txtTitle.setName(NewDocumentWindow.TXT_TITLE);
		this.txtAuthors.setName(NewDocumentWindow.TXT_AUTHORS);
		this.txtEditor.setName(NewDocumentWindow.TXT_EDITOR);
		this.btnCancel.setName(NewDocumentWindow.BTN_CANCEL);
		this.btnAdd.setName(NewDocumentWindow.BTN_CREATE);
		this.cbbCategory.setName(NewDocumentWindow.CBB_TYPE);

		// Listeners
		this.btnCancel.addActionListener(this.controller);
		this.btnAdd.addActionListener(this.controller);
		this.cbbCategory.addItemListener(this.controller);


		this.add(this.lblInfo, "span");
		
		this.add(this.lblYear, "w 80!");
		this.add(this.txtYear);
		this.add(this.lblYearError, "wrap");
		this.add(this.lblTitle, "w 80!");
		this.add(this.txtTitle);
		this.add(this.lblTitleError, "wrap");
		this.add(this.lblAuthors, "w 80!");
		this.add(this.txtAuthors);
		this.add(this.lblAuthorsError, "wrap");
		this.add(this.lblEditor, "w 80!");
		this.add(this.txtEditor);
		this.add(this.lblEditorError, "wrap");
		this.add(this.lblCategory, "w 80!");
		this.add(this.cbbCategory, "wrap");	
		
		this.add(this.pnlBook, "span");
		
		this.pnlBook.add(this.lblTypeBook, "w 80!");
		this.pnlBook.add(this.txtTypeBook, "wrap");
		this.pnlBook.add(this.lblNbPageBook, "w 80!");
		this.pnlBook.add(this.txtNbPageBook);
		
		this.pnlCD.add(this.lblTypeCD, "w 80!");
		this.pnlCD.add(this.txtTypeCD, "wrap");
		this.pnlCD.add(this.lblLenghtCD, "w 80!");
		this.pnlCD.add(this.txtLenghtCD);
		
		this.pnlDVD.add(this.lblTypeDVD, "w 80!");
		this.pnlDVD.add(this.txtTypeDVD, "wrap");
		this.pnlDVD.add(this.lblLenghtDVD, "w 80!");
		this.pnlDVD.add(this.txtLenghtDVD);
		
		this.pnlMagazine.add(this.lblNbPageMag, "w 80!");
		this.pnlMagazine.add(this.txtNbPageMag);
		
		this.pnlNewsPaper.add(this.lblDayDate, "w 80!");
		this.pnlNewsPaper.add(this.txtDayDate);
		
		this.add(this.btnCancel);
		this.add(this.btnAdd, "growx");
		this.add(this.lblInfoCreation, "cell 0 8 3 1");
		this.setTitle("Ajout d'un document");

		this.setSize(230, 310); // 217 245
		this.setModal(true);
		this.setVisible(true);
	}

  //getters
	public String getDocumentCategory() {
		return (String)this.cbbCategory.getSelectedItem();
	}

	public String getDocumentYear() {
		return this.txtYear.getText();
	}

	public String getDocumentAuthors() {
		return this.txtAuthors.getText();
	}
	
	public String getDocumentTitle() {
		return this.txtTitle.getText();
	}

	public String getDocumentEditor() {
		return this.txtEditor.getText();
	}

	public void setBookView() {
		this.removeAllView();
		this.add(this.pnlBook, "cell 0 6 2 1");
		this.add(this.btnCancel, "cell 0 7");
		this.add(this.btnAdd, "growx");
		this.add(this.lblInfoCreation, "cell 0 8 3 1");
		this.validate();
	}
	
	public void setCDView() {
		this.removeAllView();
		this.add(this.pnlCD, "cell 0 6 2 1");
		this.add(this.btnCancel, "cell 0 7");
		this.add(this.btnAdd, "growx");
		this.add(this.lblInfoCreation, "cell 0 8 3 1");
		this.validate();
	}
	
	public void setDVDView() {
		this.removeAllView();
		this.add(this.pnlDVD, "cell 0 6 2 1");
		this.add(this.btnCancel, "cell 0 7");
		this.add(this.btnAdd, "growx");
		this.add(this.lblInfoCreation, "cell 0 8 3 1");
		this.validate();
	}
	
	public void setMagView() {
		this.removeAllView();
		this.add(this.pnlMagazine, "cell 0 6 2 1");
		this.add(this.btnCancel, "cell 0 7");
		this.add(this.btnAdd, "growx");
		this.add(this.lblInfoCreation, "cell 0 8 3 1");
		this.validate();
	}
	
	public void setNewsPaperView() {
		this.removeAllView();
		this.add(this.pnlNewsPaper, "cell 0 6 2 1");
		this.add(this.btnCancel, "cell 0 7");
		this.add(this.btnAdd, "growx");
		this.add(this.lblInfoCreation, "cell 0 8 3 1");
		this.validate();
	}
	
	private void removeAllView() {
		this.remove(this.pnlBook);
		this.remove(this.pnlCD);
		this.remove(this.pnlDVD);
		this.remove(this.pnlMagazine);
		this.remove(this.pnlNewsPaper);
		this.remove(this.btnCancel);
		this.remove(this.btnAdd);
		this.remove(this.lblInfoCreation);
	}
	
	public void setInfo(String txt, boolean error) {
		this.lblInfoCreation.setText(txt);
		if(error) {
			this.lblInfoCreation.setForeground(Color.red);
		}
		else {
			this.lblInfoCreation.setForeground(Color.decode("#0C8010"));
		}
	}

	/**
	 * @return the txtNbPageBook
	 */
	public String getTxtNbPageBook() {
		return txtNbPageBook.getText();
	}

	/**
	 * @return the txtTypeBook
	 */
	public String getTxtTypeBook() {
		return txtTypeBook.getText();
	}

	/**
	 * @return the txtLenghtCD
	 */
	public String getTxtLenghtCD() {
		return txtLenghtCD.getText();
	}

	/**
	 * @return the txtTypeCD
	 */
	public String getTxtTypeCD() {
		return txtTypeCD.getText();
	}

	/**
	 * @return the txtLenghtDVD
	 */
	public String getTxtLenghtDVD() {
		return txtLenghtDVD.getText();
	}

	/**
	 * @return the txtTypeDVD
	 */
	public String getTxtTypeDVD() {
		return txtTypeDVD.getText();
	}

	/**
	 * @return the txtNbPageMag
	 */
	public String getTxtNbPageMag() {
		return txtNbPageMag.getText();
	}

	/**
	 * @return the txtDayDate
	 */
	public String getTxtDayDate() {
		return txtDayDate.getText();
	}

	/**
	 * @return the cbbCategory
	 */
	public String getCbbCategory() {
		return (String)cbbCategory.getSelectedItem();
	}


}